package org.smart4j.framework.utils;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.net.URLCodec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Wenxin on 2017/2/26.
 */
public class CodecUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(CodecUtil.class);

    private static final URLCodec URL_CODEC = new URLCodec("utf-8");

    public static String encodeURL(String source){
        try {
            return URL_CODEC.encode(source);
        } catch (EncoderException e) {
            LOGGER.debug("encode url failure: {}",e);
            throw new RuntimeException(e);
        }
    }

    public static String decodeURL(String source){
        try {
            return URL_CODEC.decode(source);
        } catch (DecoderException e) {
            LOGGER.debug("decode url failure: {}",e);
            throw new RuntimeException(e);
        }
    }

}
